<template>
  <div class="recipe-card" @click="$emit('click')">
    <div class="recipe-image">
      <van-image
        :src="recipe.imgUrl || defaultImage"
        fit="cover"
        alt="食谱图片"
        @click.stop="showImagePreview"
      >
        <template #error>
          <div class="image-error">
            <van-icon name="photo" />
          </div>
        </template>
      </van-image>
      <div class="recipe-overlay">
        <div class="recipe-rating">
          <van-rate
            v-model="recipe.avgRating"
            readonly
            color="#ff9900"
            :size="18"
            allow-half
          />
          <span class="rating-text">{{ recipe.avgRating }}</span>
        </div>
      </div>
    </div>
    <div class="recipe-info">
      <h3 class="recipe-title">{{ recipe.name }}</h3>
      <div class="recipe-meta">
        <span class="recipe-author">作者: {{ recipe.author || "未知" }}</span>
        <span class="recipe-cuisine">{{
          recipe.cuisineType || "中式料理"
        }}</span>
      </div>
      <div class="recipe-tags">
        <van-tag v-if="recipe.tasteType" type="success" plain size="medium">{{
          recipe.tasteType
        }}</van-tag>
        <van-tag v-if="recipe.cookingTime" type="primary" plain size="medium"
          >{{ recipe.cookingTime }}分钟</van-tag
        >
        <van-tag v-if="recipe.difficulty" type="warning" plain size="medium">{{
          difficultyText
        }}</van-tag>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import { showImagePreview } from "vant";

const props = defineProps({
  recipe: {
    type: Object,
    required: true,
  },
});

defineEmits(["click"]);

const defaultImage = ref("/images/default-recipe.jpg");

const difficultyText = computed(() => {
  const difficultyMap = {
    1: "简单",
    2: "中等",
    3: "困难",
  };
  return difficultyMap[props.recipe.difficulty] || "中等";
});

const showImagePreview = () => {
  if (props.recipe.imgUrl) {
    showImagePreview({
      images: [props.recipe.imgUrl],
      startPosition: 0,
      closeable: true,
    });
  }
};
</script>

<style scoped>
.recipe-card {
  width: 220px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #fff;
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  margin: 10px;
  cursor: pointer;
}

.recipe-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.recipe-image {
  position: relative;
  height: 150px;
  overflow: hidden;
}

.recipe-image :deep(.van-image) {
  width: 100%;
  height: 100%;
}

.image-error {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #f5f7fa;
  color: #909399;
  font-size: 30px;
}

.recipe-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.7), transparent);
  padding: 10px;
  transition: opacity 0.3s;
}

.recipe-rating {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.rating-text {
  margin-left: 5px;
  color: #fff;
  font-size: 14px;
}

.recipe-info {
  padding: 15px;
}

.recipe-title {
  margin: 0 0 10px;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recipe-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 12px;
  color: #909399;
}

.recipe-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
</style>
